package community.hospital.common;

import cn.hutool.jwt.JWTException;
import community.hospital.enums.BusinessEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author：ydj
 * @Date：2023/1/16 15:32
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandle {

    @ExceptionHandler(BindException.class)
    public <T> R<T> exceptionHandler(BindException e) {
        log.error(e.getMessage());
        return R.error(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

    @ExceptionHandler(JWTException.class)
    public R<String> exceptionHandler(JWTException e) {
        log.error(e.getMessage());
        return R.error("请勿携带非法token");
    }

    @ExceptionHandler(CustomerException.class)
    public <T> R<T> exceptionHandler(CustomerException e) {

        log.error(e.getBusinessEnum().getMsg());
        return R.error(e.getMsg());
    }


}
